package cn.edu.sgu.www.mhxysy.service.baoshi.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.baoshi.EquipmentViewBaoshiDTO;
import cn.edu.sgu.www.mhxysy.entity.baoshi.EquipmentViewBaoshi;
import cn.edu.sgu.www.mhxysy.entity.equipment.EquipmentType;
import cn.edu.sgu.www.mhxysy.mapper.baoshi.EquipmentViewBaoshiMapper;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentTypeMapper;
import cn.edu.sgu.www.mhxysy.pager.baoshi.EquipmentBaoshiPager;
import cn.edu.sgu.www.mhxysy.service.baoshi.EquipmentViewBaoshiService;
import cn.edu.sgu.www.mhxysy.util.CollectionUtils;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class EquipmentViewBaoshiServiceImpl implements EquipmentViewBaoshiService {

    private final EquipmentTypeMapper equipmentTypeMapper;
    private final EquipmentViewBaoshiMapper equipmentViewBaoshiMapper;

    public EquipmentViewBaoshiServiceImpl(
            EquipmentTypeMapper equipmentTypeMapper,
            EquipmentViewBaoshiMapper equipmentViewBaoshiMapper) {
        this.equipmentTypeMapper = equipmentTypeMapper;
        this.equipmentViewBaoshiMapper = equipmentViewBaoshiMapper;
    }

    @Override
    public void insert(EquipmentViewBaoshiDTO equipmentViewBaoshiDTO) {
        EquipmentViewBaoshi equipmentViewBaoshi = toEntity(equipmentViewBaoshiDTO);

        equipmentViewBaoshi.setId(null);

        equipmentViewBaoshiMapper.insert(equipmentViewBaoshi);
    }

    @Override
    public void updateById(EquipmentViewBaoshiDTO equipmentViewBaoshiDTO) {
        ObjectUtils.requireNonNull(equipmentViewBaoshiDTO.getId(), CustomMessages.ID_NOT_NULL);

        EquipmentViewBaoshi equipmentViewBaoshi = toEntity(equipmentViewBaoshiDTO);

        equipmentViewBaoshiMapper.updateById(equipmentViewBaoshi);
    }

    @Override
    public List<String> selectInlayEquipments(Integer baoshiId) {
        QueryWrapper<EquipmentViewBaoshi> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("baoshi_id", baoshiId);

        List<EquipmentViewBaoshi> list = equipmentViewBaoshiMapper.selectList(queryWrapper);

        if (CollectionUtils.isNotEmpty(list)) {
            // 通过宝石类型ID查询装备类型ID列表
            List<Integer> equipmentIds = list.stream().map(new Function<EquipmentViewBaoshi, Integer>() {
                @Override
                public Integer apply(EquipmentViewBaoshi equipmentBaoshi) {
                    return equipmentBaoshi.getEquipmentId();
                }
            }).collect(Collectors.toList());

            // 查询装备类型，得到装备类型名称列表
            List<EquipmentType> equipmentTypes = equipmentTypeMapper.selectBatchIds(equipmentIds);

            return equipmentTypes.stream().map(new Function<EquipmentType, String>() {
                @Override
                public String apply(EquipmentType equipmentType) {
                    return equipmentType.getName();
                }
            }).collect(Collectors.toList());
        }

        return new ArrayList<>();
    }

    @Override
    public Page<EquipmentViewBaoshi> selectByPage(EquipmentBaoshiPager pager) {
        QueryWrapper<EquipmentViewBaoshi> queryWrapper = new QueryWrapper<>();
        Page<EquipmentViewBaoshi> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getBaoshiId() != null,
                "baoshi_id", pager.getBaoshiId()
        );
        queryWrapper.eq(
                pager.getEquipmentId() != null,
                "equipment_id", pager.getEquipmentId()
        );

        return equipmentViewBaoshiMapper.selectPage(page, queryWrapper);
    }

    private EquipmentViewBaoshi toEntity(EquipmentViewBaoshiDTO equipmentViewBaoshiDTO) {
        EquipmentViewBaoshi equipmentViewBaoshi = new EquipmentViewBaoshi();

        equipmentViewBaoshi.setId(equipmentViewBaoshiDTO.getId());
        equipmentViewBaoshi.setBaoshiId(equipmentViewBaoshiDTO.getBaoshiId());
        equipmentViewBaoshi.setEquipmentId(equipmentViewBaoshiDTO.getEquipmentId());

        return equipmentViewBaoshi;
    }

}